package com.atguigu.boot.controller;

import com.atguigu.boot.bean.Person;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParameterTestController {
    /**
     * 数据绑定：页面提交的数据（GET、POST）都可以和对象属性绑定
     * @param person
     * @return
     */
    @PostMapping("/saveUser")
    public Person saveUser(Person person){

        return person;
    }

    @GetMapping("/car/{id}/owner/{userName}")
    public Map getCar(@PathVariable("id") Integer id,
                      @PathVariable("userName") String userName,
                      @PathVariable Map pv,
                      @RequestHeader("user-agent") String userAgent,
                      @RequestHeader Map headers,
                      @RequestParam("age") Integer age,
                      @RequestParam("inters")List< String> inters,
                      @RequestParam Map intersMap,
                      @CookieValue(value = "_ga", required = false) String _ga){
        Map map = new HashMap();
        /*map.put("id",id);
        map.put("userName",userName);
        map.put("pv",pv);
        map.put("user-agent",userAgent);
        map.put("headers",  headers);*/
        map.put("age",age);
        map.put("inters",inters);
        map.put("intersMap",intersMap);
        map.put("_ga",_ga);
        return map;

    }

    @PostMapping("/save")
    public void methodSave(@RequestBody String conntent){
        System.out.println(conntent);

    }

    // /car/sell;low=34;brand=byd,audi,yd
    // springboot默认禁用矩阵变量功能，需要手动开启
    // 原理：对于路径的处理，在配置类中重写WebMvcConfigurer的 webMvcConfigurer()方法， 设置UrlPathHelper 的 removeSemicolonContent=false,支持矩阵变量
    @GetMapping("/cars/{path}")
    public Map CarsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") String brand,
                        @PathVariable String path ){
        Map<String, Object> map = new HashMap();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value ="age" , pathVar = "bossId") Integer bosAge,
                    @MatrixVariable(value ="age" , pathVar = "empId")  Integer empAge){
        Map<String, Object> map = new HashMap();
        map.put("bosAge",bosAge);
        map.put("empAge",empAge);
        return map;
    }
}
